package main.Concurrent.Map;

import java.util.ArrayList;
import java.util.List;

import main.Concurrent.Iteration.IterationOperation;

public class MapOperation<E, R> implements IterationOperation<E, R, List<R>> {
    private List<E> source;
    private List<R> result;
    private MapFunction<E, R> f;
    
    /**
     * @param source Source list
     * @param f
     */
    public MapOperation(List<E> source, MapFunction<E, R> f) {
        this.source = source;
        this.result = new ArrayList<R>();
        for(int i = 0; i < source.size(); i++) {
            result.add(null);
        }
        this.f = f;
    }

    @Override
    public R compute(int index, E val) {
        return this.f.map(this.source.get(index));
    }

    @Override
    public List<R> aggregate(List<R> current, int index, R result) {
        current.set(index, result);
        return current;
    }
}
